package logutils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ParseLog {
	public ArrayList<String> readLog(String name) {
		ArrayList<String> l = new ArrayList<String>();
		File f = new File(name);
		FileHandler fh = new FileHandler();

		try {
			l = fh.read(f, l);
		} catch (Exception e) {
			System.out.println("Caught this" + e);
		}
		return l;
	}

	/*
	 * get agents
	 */
	public String[] getAgentList(ArrayList<String> l){
			String[] agentList = new String[8];
			Pattern agent = Pattern
			.compile("((\\d{10}),a,\\S+,(\\d+|-\\d+).*)");
			int i = 0;
			Iterator<String> lc = l.iterator();
			while (lc.hasNext()) {
				Matcher matchedAgent = agent.matcher(lc.next());
				while (matchedAgent.find()) {				
					 //System.out.println("Found agent ID " +	 matchedAgent.group(3)); 
					 agentList[i] = matchedAgent.group(3);
					 i++;
				
				}
			}
			return agentList;
	}
	/*
	 * getAuctions returns map (key = auctionID, value = day:type)
	 */
	public HashMap<String, String> getAuctions(ArrayList<String> l) {
		Pattern auction = Pattern
				.compile("((\\d{10}),u,(\\d+),(\\d+),(\\d+).*)");
		HashMap<String, String> auctionMap = new HashMap<String, String>();
		int day;
		int type;
		Iterator<String> lc = l.iterator();
		while (lc.hasNext()) {
			Matcher matchedAuction = auction.matcher(lc.next());
			while (matchedAuction.find()) {

				/*
				 * System.out.println("Found auctionid " +
				 * matchedAuction.group(3)); System.out.println("Found resource " +
				 * matchedAuction.group(4)); System.out.println("Found day " +
				 * matchedAuction.group(5));
				 */

				day = Integer.parseInt(matchedAuction.group(5)) - 1;
				type = Integer.parseInt(matchedAuction.group(4)) - 4;
				// Only get the ent auctions.
				if (type >= 0) {
					auctionMap.put(matchedAuction.group(3), Integer
							.toString(day)
							+ ":" + Integer.toString(type));
				}
			}
		}
		return auctionMap;
	}

	/*
	 * Returns client preferences for this agent as an integer array Set to 0 if
	 * the client isn't staying Set to 1 if they are staying Clients numbered 0
	 * ...7 Days numbered 0 .. 4
	 */
	public int[][] getClientPrefs(ArrayList<String> l, String agentId) {
		Pattern pc = Pattern.compile("((\\d{10}),c,\\d+," + agentId + ",(.*))");

		int[][] clientPrefs = new int[8][5];

		Iterator<String> la = l.iterator();
		while (la.hasNext()) {
			Matcher m = pc.matcher(la.next());
			while (m.find()) {
				// System.out.println("Found " + m.group(1));
				// System.out.println("Found " + m.group(2) + m.group(3));
				String[] prefs = m.group(3).split(",");
				/*
				 * Assume 8 clients
				 */
				int max = 48;
				int j = 0;
				for (int i = 0; i < max; i = i + 6) {
					/*
					 * System.out.println(" First " + prefs[i] + " Second " +
					 * prefs[i+1] + "index " + i);
					 */
					int start = Integer.parseInt(prefs[i]) - 1;// first night
																// avail for ent
					int end = Integer.parseInt(prefs[i + 1]) - 1; // Don't
																	// stay over
																	// on last
																	// night -
																	// not avail
																	// for ent
					for (int mind = start; mind < end; mind++) {
						clientPrefs[j][mind] = 1;
					}
					j++;
				}
			}
		}
		return clientPrefs;
	}

	public int[][] getEntCostsPrefs(ArrayList<String> l, String agentId) {
		Pattern pc = Pattern.compile("((\\d{10}),c,\\d+," + agentId + ",(.*))");
		/*
		 * Initialise to -1
		 */
		int[][] entCosts = new int[8][3];

		Iterator<String> la = l.iterator();
		while (la.hasNext()) {
			Matcher m = pc.matcher(la.next());
			while (m.find()) {
				// System.out.println("Found " + m.group(1));
				// System.out.println("Found " + m.group(2) + m.group(3));
				String[] prefs = m.group(3).split(",");
				/*
				 * Assume 8 clients
				 */
				int max = 48;
				int client = 0;
				int k;
				for (int i = 0; i < max; i = i + 6) {
					/*
					 * System.out.println(" First " + prefs[i] + " Second " +
					 * prefs[i+1] + "index " + i);
					 */
					k = i + 3;
					for (int ent = 0; ent < 3; ent++) {
						entCosts[client][ent] = Integer.parseInt(prefs[k]);
						k++;
					}
					client++;
				}
			}
		}
		return entCosts;
	}

	/*
	 * Day 0 = Monday, 1= Tues etc Ent 0 = AW, 1= AM, 2= MU
	 */
	public int[][] getInitialTicketAllocations(ArrayList<String> l,
			String agentId, HashMap<String, String> auctionMap) {

		Iterator<String> lb = l.iterator();
		String[] tmp;
		int[][] ticketsInitial = new int[5][3];
		Pattern en = Pattern.compile("((\\d{10}),t," + agentId
				+ ",auction,(\\d+),(\\d+)(.*))"); // transaction

		int totalTickets = 0;
		while (lb.hasNext()) {
			Matcher mn = en.matcher(lb.next());
			if (totalTickets < 12) {
				while (mn.find()) {
					// System.out.println("Found auction id " + mn.group(3));
					// System.out.println("Found number " + mn.group(4));
					tmp = auctionMap.get(mn.group(3)).split(":");

					int day = Integer.parseInt(tmp[0]);
					int ent = Integer.parseInt(tmp[1]);
					// System.out.println("Auction " + mn.group(3) + " ent "+
					// ent + " " + day);
					if (ent >= 0) {
						ticketsInitial[day][ent] = ticketsInitial[day][ent]
								+ Integer.parseInt(mn.group(4));
						/*
						 * System.out.println("Entertainment allocated " + ent + "
						 * day " + day + " Num tickets " +
						 * ticketsInitial[day][ent]);
						 */
						totalTickets = totalTickets + ticketsInitial[day][ent];
					}
				}
			}
		}
		return ticketsInitial;
	}

	public int[][] getFinalEntAlloc(ArrayList<String> l, String agentId) {
		Pattern pc = Pattern.compile("((\\d{10}),l,\\d+," + agentId + ",(.*))");

		int[][] entsFinal = new int[8][3];

		Iterator<String> la = l.iterator();
		while (la.hasNext()) {
			Matcher m = pc.matcher(la.next());
			while (m.find()) {
				// System.out.println("Found " + m.group(1));
				// System.out.println("Found " + m.group(2) + m.group(3));
				String[] prefs = m.group(3).split(",");
				/*
				 * Assume 8 clients
				 */
				int max = 48;
				int client = 0;
				int k;
				for (int i = 0; i < max; i = i + 6) {
					/*
					 * System.out.println(" First " + prefs[i] + " Second " +
					 * prefs[i+1] + "index " + i);
					 */
					k = i + 3;
					for (int ent = 0; ent < 3; ent++) {
						entsFinal[client][ent] = Integer.parseInt(prefs[k]);
						// System.out.println("Client " + client+" Ent "+ent+"
						// Day "+prefs[k]);
						k++;
					}
					client++;
				}
			}
		}
		return entsFinal;
	}

	public int[][] getActualClientStays(ArrayList<String> l, String agentId) {
		Pattern pc = Pattern.compile("((\\d{10}),l,\\d+," + agentId + ",(.*))");

		int[][] clientStays = new int[8][5];

		Iterator<String> la = l.iterator();
		while (la.hasNext()) {
			Matcher m = pc.matcher(la.next());
			while (m.find()) {
				// System.out.println("Found " + m.group(1));
				// System.out.println("Found " + m.group(2) + m.group(3));
				String[] prefs = m.group(3).split(",");
				/*
				 * Assume 8 clients
				 */
				int max = 48;
				int j = 0;
				for (int i = 0; i < max; i = i + 6) {
					/*
					 * System.out.println(" First " + prefs[i] + " Second " +
					 * prefs[i+1] + "index " + i);
					 */
					int start = Integer.parseInt(prefs[i]) - 1;
					int end = Integer.parseInt(prefs[i + 1]) - 1;

					for (int mind = start; mind < end; mind++) {
						clientStays[j][mind] = 1;

					}
					j++;
				}
			}
		}
		return clientStays;
	}
}
